<?php
/***
 * 描述：产品库存管理[店长权限]
 * @category		Admin		
 * @package			Action			
 * @subpackage			
 * @author			wanght <whtaok@gmail.com>
 * @since			2013-4-6
 * @version			$Id: ProductStockManageAction.class.php 3 2013-10-14 02:49:41Z whtaok@gmail.com $
 */
class ProductStockManageAction extends AdminAction {
	
	/**
	 *
	 * @access		protected
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	protected  function _initialize() {
		parent::_initialize();
		$this->usingModel="ProductPrice";
	}
	
	/**
	 * 商品库存信息查询首页面
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function indexAct() {
		$this->showStockListAct();
	}
	
	/**
	 * 商品库存信息查询页面
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function showStockListAct(){
		$cateCode = I('cate_code');
		$search = array(
				'cate_code'=> I('cate_code'),
				'name'=>I('name'),
				'barcode'=>I('barcode'),
				'is_show'=>I('is_show'),
				'is_warn'=>I('is_warn'),
				'stock_qty'=>I('stock_qty'),
		);
		//设置条件
		$modWhere = array(
				'is_show'=>$search['is_show'],
				'spec_qty'=>'1',
		);
		//分类条件
		if($search['cate_code']){
			$modWhere['cate_code2'] = array('like',$search['cate_code'].'%');
		}
		//商品名称
		if($search['name']){
			$modWhere['name'] = array('like','%'.$search['name'].'%');
		}
		//商品代码
		if($search['barcode']){
			$modWhere['barcode'] = array('like','%'.$search['barcode'].'%');
		}
		//是否报警
		if($search['is_warn']=='1'){
			$modWhere['_string'] = 'ProductPrice.stock_qty <= ProductPrice.stock_warn_qty';
		}
		//按库存量查询
		if($search['stock_qty']=='0'){
			$modWhere['stock_qty'] = '0';
		}else if($search['stock_qty']>0){
			$modWhere['stock_qty'] = array('egt',$search['stock_qty']);
		}
	
		//有商品运价的查询
		$viewMod = new ProductPriceViewModel();
		$viewMod->buildPricedFields($this->curr_shop_id);
		$totalRows = $viewMod->setModWhere($modWhere)->count();
		list($list,$page)=$viewMod->setModWhere($modWhere)->listPage($totalRows);
		$this->assign('list',$list);
		$this->assign('page',$page);
		$this->assign('search',$search);
		$this->assign('cateTree',productcate_tree());
		$this->display('list_stock');
	}
	
	/**
	 * 增加指定店铺和商品ID的商品库存
	 * 该操作记录日志到商品变更表中
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doAddStockQtyAct(){
		$mod = new ProductPriceModel();
		$modWhere = array(
			'shop_id'=>$this->curr_shop_id,
			'prod_id'=>I('prod_id')
		);
		$detail = $mod->addStockQty($modWhere['shop_id'],$modWhere['prod_id'],I('add_stock_qty'));
		if($detail){
			$rtnData = array(
					'error'=>0,
					'message'=>'更新成功',
					'stock_qty'=>$detail['stock_qty']
			);
		}else{
			$rtnData = array(
					'error'=>1,
					'message'=>$mod->getError(),
			);
		}
		$this->ajaxReturn($rtnData);
	}
	
	/**
	 * 更新指定店铺和商品ID的商品库存警告值
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doUpdStockWarnQtyAct(){
		$mod = new ProductPriceModel();
		$modWhere = array(
				'shop_id'=>$this->curr_shop_id,
				'prod_id'=>I('prod_id')
		);
		$flag = $mod->updStockWarnQty($modWhere['shop_id'],$modWhere['prod_id'],I('add_stock_qty'));
		if($flag){
			$rtnData = array(
					'error'=>0,
					'message'=>'更新成功',
			);
		}else{
			$rtnData = array(
					'error'=>1,
					'message'=>$mod->getError()
			);
		}
		$this->ajaxReturn($rtnData);
	}
}

?>